package com.itheima.mall.service.smsService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.commons.Code;
import com.itheima.mall.commons.R;
import com.itheima.mall.exception.ConsumerException;
import com.itheima.pojo.pms.PmsProduct;
import com.itheima.pojo.sms.SmsFlashPromotionProductRelation;
import com.itheima.vo.PmsProVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class SmsFlashPromotionProductRelationService {

    @DubboReference
    private SmsFlashPromotionProductRelationServiceApi smsFlashPromotionProductRelationServiceApi;


    /**
     * 批量选择商品添加关联
     */
    public R addFlashProductRelation(SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        smsFlashPromotionProductRelationServiceApi.addFlashProductRelation(smsFlashPromotionProductRelation);
        return new R(Code.SUCCESS,"新增商品关联成功");
    }

    /**
     * 删除商品关联
     * id
     */
    public R deleteFlashProductRelation(Integer id) {
        if (ObjectUtils.isEmpty(id)){
            throw new ConsumerException("请输入ID!");
        }
        smsFlashPromotionProductRelationServiceApi.deleteFlashProductRelation(id);
        return new R(Code.SUCCESS,"删除成功!");
    }
    /**
     * 分页查询不同场次关联及商品信息
     * flashPromotionId @param
     * flashPromotionSessionId  @param
     * 当前页码 @param pageNum
     * 当前页条目数 @param pageSize
     * @return
     */
  /*  public PageResult<PmsProVo> selectByPage(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {

        IPage<PmsProVo> page = new Page<>(pageNum, pageSize);
        List<SmsFlashPromotionProductRelation> smsFlashPromotionProductRelationList=smsFlashPromotionProductRelationServiceApi.list(page,flashPromotionId,flashPromotionSessionId);
        for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : smsFlashPromotionProductRelationList) {
            PmsProVo pmsProVo=new PmsProVo();
            BeanUtil.copyProperties(smsFlashPromotionProductRelation,pmsProVo);

            Long productId = smsFlashPromotionProductRelation.getProductId();
            PmsProduct pmsProduct = pmsProductServiceApi.selectProductByIds(productId);
            pmsProVo.setProduct(pmsProduct);
        }

        PageResult<PmsProVo> result=new PageResult<>(page);
        return result;

    }*/

    /**
     * 修改关联信息
     * @param id
     * @param smsFlashPromotionProductRelation
     * @return
     */
    public R update(Long id, SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        smsFlashPromotionProductRelationServiceApi.update(id,smsFlashPromotionProductRelation);
        return R.ok("操作成功!");
    }

    /**
     * 获取管理商品促销信息
     * @param id
     * @return
     */
    public R selectProductionMessage(Long id) {
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation =smsFlashPromotionProductRelationServiceApi.selectProductionMessage(id);
        return R.ok(smsFlashPromotionProductRelation);
    }
}
